package com.scs.application.core.service;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.scs.application.core.dto.ExcelImportField;
import com.scs.application.core.entity.BaseEntity;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.export.entity.ExportParams;
import com.scs.application.core.query.QueryContext;
import com.scs.application.modules.sys.entity.Flow;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
public interface IBaseService<T extends BaseEntity> extends IService<T> {

    /**
     * 通用分页查询
     * @param context
     * @return
     */
    Page<?> page(QueryContext context);

    /**
     * 通用列表查询
     * @param context
     * @return
     */
    List<?> list(QueryContext context);

    @Transactional(rollbackFor = Exception.class)
    default boolean insertCascadeById(T entity){
        return this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    default boolean updateCascadeById(T entity){
        return this.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    default boolean removeCascadeById(Serializable id){
        return this.removeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    default boolean removeCascadeByIds(Collection<? extends Serializable> idList){
        return this.removeByIds(idList);
    }

    /**
     *
     * @param modelClass
     * @param entityList
     * @param batchSize
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    <R> boolean saveBatch(Class<R> modelClass,Collection<R> entityList, int batchSize);


    /**
     * 处理流程改变
     */
    void handleFlowChange();

    /**
     * 导出数据
     * @param context
     * @param exportParams
     * @return
     */
    String exportFullData(QueryContext context, ExportParams exportParams, Map<String, Object> userParam) throws Exception;

    @Transactional(rollbackFor = Exception.class)
    void saveBatchImport(Class<?> modelClass, Collection<?> entityList, int batchSize);

    void  closeBatch(List<?> entityList, String closeRemark, String... flowStatusFilterList);

    /**
     * @Description： 批量作废，只修改主单据状态，增加流程日志
     * @param entityList 继承FlowEntity的子类列表   【必须】
     * @param closeRemark 作废原因，不传则为 无原因作废
     * @param flowStatusFilterList 哪些状态可以作废
     */
    void  addFlowLogBatch(List<?> entityList, String remark, Flow flow);

    //数据视图顶部合计
    Map<String,Double> sum(QueryContext context,String sumFields);

    void handleFlowChangeBatch();
    default String importData(List<JSONObject> dataList){
        BusinessException.throwErrorIf(true, "导入尚未实现");
        return null;
    };


    void excelDownloadTemp(HttpServletResponse response, Map<String, String> reqData,List<ExcelImportField> exportHeadListParam);

    String excelImport(MultipartFile file, Map<String, String> reqData);
}
